home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / TABLE2.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  3KB  |  112 lines

  1. /*
  2.  * Copyright (c) 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Associative map between objects.
  25.  */
  26.  
  27. #include <InterViews\table2.h>
  28.  
  29. class Table2Entry {
  30.     friend class Table2;
  31.  
  32.     void* key1;
  33.     void* key2;
  34.     void* value;
  35.     Table2Entry* chain;
  36. };
  37.  
  38. Table2::Table2 (int n) {
  39.     register Table2Entry** e;
  40.  
  41.     size = 32;
  42.     while (size < n) {
  43.     size <<= 1;
  44.     }
  45.     first = new Table2Entry*[size];
  46.     --size;
  47.     last = &first[size];
  48.     for (e = first; e <= last; e++) {
  49.     *e = nil;
  50.     }
  51. }
  52.  
  53. Table2::~Table2 () {
  54.     delete first;
  55. }
  56.  
  57. inline Table2Entry* Table2::Probe (void* i1, void* i2) {
  58.     return first[((unsigned int)i1 + (unsigned int)i2) & size];
  59. }
  60.  
  61. inline Table2Entry** Table2::ProbeAddr (void* i1, void* i2) {
  62.     return &first[((unsigned int)i1 + (unsigned int)i2) & size];
  63. }
  64.  
  65. void Table2::Insert (void* k1, void* k2, void* v) {
  66.     register Table2Entry* e;
  67.     register Table2Entry** a;
  68.  
  69.     e = new Table2Entry;
  70.     e->key1 = k1;
  71.     e->key2 = k2;
  72.     e->value = v;
  73.     a = ProbeAddr(k1, k2);
  74.     e->chain = *a;
  75.     *a = e;
  76. }
  77.  
  78. boolean Table2::Find (void*& v, void* k1, void* k2) {
  79.     register Table2Entry* e;
  80.  
  81.     for (e = Probe(k1, k2); e != nil; e = e->chain) {
  82.     if (e->key1 == k1 && e->key2 == k2) {
  83.         v = e->value;
  84.         return true;
  85.     }
  86.     }
  87.     return false;
  88. }
  89.  
  90. void Table2::Remove (void* k1, void* k2) {
  91.     register Table2Entry* e, * prev;
  92.     Table2Entry** a;
  93.  
  94.     a = ProbeAddr(k1, k2);
  95.     e = *a;
  96.     if (e != nil) {
  97.     if (e->key1 == k1 && e->key2 == k2) {
  98.         *a = e->chain;
  99.         delete e;
  100.     } else {
  101.         do {
  102.         prev = e;
  103.         e = e->chain;
  104.         } while (e != nil && (e->key1 != k1 || e->key2 != k2));
  105.         if (e != nil) {
  106.         prev->chain = e->chain;
  107.         delete e;
  108.         }
  109.     }
  110.     }
  111. }
  112.